﻿using System.Text.RegularExpressions;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using Me.Amon.M;

namespace Me.Amon.Util
{
    public class WebUtil
    {
        public static MUser CurrentUser(HttpSessionState session)
        {
            var user = session["amon"] as MUser;
            if (user == null)
            {
                user = new MUser();
                session["amon"] = user;
            }
            return user;
        }

        public static bool IsValid(string input, string display, int length, Label label)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                label.Text = string.Format("请输入 {0} ！", display);
                return false;
            }
            if (input.Length > length)
            {
                label.Text = string.Format("{0}不能超过 {1} 个字符！", display, length);
                return false;
            }
            return true;
        }

        public static bool IsValid(string input, string display, Label label)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                label.Text = string.Format("请输入 {0} ！", display);
                return false;
            }
            if (!Regex.IsMatch(input, "^\\d+$"))
            {
                label.Text = string.Format("{0} 应为一个非负整数！", display);
                return false;
            }
            return true;
        }
    }
}